<?php

namespace common\logics\trade;

use common\helpers\Curl;
use Yii;
use yii\db\Exception;
use common\models\shop\ShopOrder;
use common\models\shop\ShopOrderLog;
use common\models\shop\ShopGoodsStatistic;
use common\models\trade\TradeRefund as TableTradeRefund;
use common\helpers\Utils;
use common\models\shop\OrderRefund;
use common\models\trade\Trade;
use common\models\trade\TradeOrder;
use common\models\trade\TradePartner;
use SoapClient;
use common\helpers\ArrayHelper;

/**
 * 交易成功后异步通知业务处理
 * BusinessNotify
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
trait BusinessNotifyTrait {

    /**
     * 商城业务更新
     * @param \common\models\trade\Trade|null $trade
     */
    protected function notifyToShop(&$msg) {
        $trade = $this->trade;

        $busorder = ShopOrder::findByBizno($trade->bizno);
        if ($busorder == null) {
            $msg = '商城业务订单【' . $trade->bizno . '】不存在！';
            return false;
        }
        $busorder->out_trade_no = $trade->trade_no;
        $busorder->pay_time = $trade->updated_at;
        $busorder->point = ceil($trade->credit * 10);
        $busorder->point_money = $trade->credit;
        if ($busorder->status == ShopOrder::STATUS_DEFAULT) {
            $busorder->status = ShopOrder::STATUS_SUCCESS;
        }
        if (!$busorder->save()) {
            Yii::error("\n" . \yii\helpers\VarDumper::export($busorder->errors), 'payment');
            $msg = '商城业务订单保存失败！';
            return false;
        }
        //日志
        $busorderLog = new ShopOrderLog();
        $busorderLog->user_id = $trade->user_id;
        $busorderLog->action = ShopOrderLog::ACTION_PAY;
        $busorderLog->order_id = $busorder->id;
        $busorderLog->is_admin = 1;
        $busorderLog->mask = '订单支付成功';
        if (!$busorderLog->save()) {
            $msg .= "\n商城日志信息保存失败。";
            Yii::error("\n" . \yii\helpers\VarDumper::export($busorderLog->errors), 'payment');
        }
        //商城订单商品
        if (!empty($busorder->orderGoods)) {
            foreach ($busorder->orderGoods as $goods) {
                $statistic = ShopGoodsStatistic::find()->where(['goods_id' => $goods->goods_id])->one();
                $statistic->widgets += ShopGoodsStatistic::WIDGETS_PAY;
                if (!$statistic->save()) {
                    $msg .= "\n商城订单货品统计信息保存失败。";
                    Yii::error("\n" . \yii\helpers\VarDumper::export($statistic->errors), 'payment');
                }
            }
        }
        return true;
    }

    /**
     * 通知索软票务
     * @param \common\models\trade\Trade|null $trade
     */
    protected function notifyToSrticket(&$msg) {
        $trade = $this->trade;

        $params = [
            'api_secret' => $trade->partner->sign,
            'OrderNo' => $trade->bizno,
            'Transactionnum' => $trade->trade_no
        ];

        $wsdl = 'http://middle.ticket.lnts.com.cn/WebTicketService.asmx?WSDL';
        $client = new SoapClient($wsdl, [
            'trace' => 1,
            'exception' => 0
        ]);

        $result = $client->ConfirmOrder($params);

        Yii::error($result);

        $result = get_object_vars($result);

        return ArrayHelper::getValue($result, 'ConfirmOrderResult');
    }

}
